package com.globisoft.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import com.globisoft.domain.GRN;
import com.globisoft.service.GoodReceivedNoteService;
import com.globisoft.util.ComboBoxObject;

@Controller
public class GRNController {

	@Autowired
	GoodReceivedNoteService goodReceivedNoteService;
	
	public void setGoodReceivedNoteService(
			GoodReceivedNoteService goodReceivedNoteService) {
		this.goodReceivedNoteService = goodReceivedNoteService;
	}

	
	@RequestMapping(value = "/grn", method = RequestMethod.GET )
	public ModelAndView getGRN(){
		GRN grn = new GRN();
		
		return new ModelAndView("grn","grn",grn);
	}
	
	@RequestMapping(value = "/grn", method = RequestMethod.POST )
	public ModelAndView saveGRN(@ModelAttribute("grn")GRN grn, BindingResult result){
		System.out.println(grn);
		goodReceivedNoteService.saveGRN(grn);
		return new ModelAndView();
	}
	
	
	@ModelAttribute("GRNTypeList")
	public List populateAccountAttributeList() {
		
		List<ComboBoxObject> typeList = goodReceivedNoteService.getAllGRNType();
		return typeList;
	}
	

	@ModelAttribute("GRNStatusList")
	public List populateGRNStatusList() {
		List<ComboBoxObject> statusList = new ArrayList<ComboBoxObject>();
		statusList.add(new ComboBoxObject(1,"Primary"));
		statusList.add(new ComboBoxObject(2,"Approved"));
		statusList.add(new ComboBoxObject(3,"Cancelled"));
		statusList.add(new ComboBoxObject(4,"Processed"));
		return statusList;
	}
	

	@ModelAttribute("purchaseOrderList")
	public List populatePurchaseOrderList() {
		List<ComboBoxObject> purchaseOderList = goodReceivedNoteService.getAllPurchaseOrder();
		return purchaseOderList;
	}
	

	@RequestMapping(value = "/getSupplier.html", method = RequestMethod.GET)
	public @ResponseBody String getsupplier(@RequestParam("purchaseOrderId") int purchaseOrderId) {
	    String result = "";
	    try {
	      
	      List<ComboBoxObject> supplierList = goodReceivedNoteService.getSuppliersByPurchaseOrder(purchaseOrderId);
	      for (ComboBoxObject supplier : supplierList) {
	        result += "<option value='" + supplier.getId() + "'>" + supplier.getValue() + "</option>";
	      }
	    }
	    catch (Exception ex) {
	      return "error";
	    }
	    return result;
	  }
	
	/*@ModelAttribute("supplierList")
	public List populateSupplierList() {
		List<ComboBoxObject> supplierList = new ArrayList<ComboBoxObject>();
		supplierList.add(new ComboBoxObject(11,"Supplier 1"));
		supplierList.add(new ComboBoxObject(12,"Supplier 2"));
		supplierList.add(new ComboBoxObject(13,"Supplier 3"));
		return supplierList;
	}*/
	

	@ModelAttribute("priceList")
	public List populatePriceList() {
		List<ComboBoxObject> priceList = new ArrayList<ComboBoxObject>();
		priceList.add(new ComboBoxObject(1,"Price Lis 1"));
		priceList.add(new ComboBoxObject(2,"Price Lis 2"));
		priceList.add(new ComboBoxObject(3,"Price Lis 3"));
		return priceList;
	}
}
